package com.bytedance.scenedemo.ability_compatibility.softkeyboard;

import com.bytedance.scene.ktx.SceneExtensions;
import com.bytedance.scene.ui.template.AppCompatScene;
import com.bytedance.scenedemo.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;

import static com.bytedance.scenedemo.ability_compatibility.softkeyboard.SoftKeyboardDemoScene.focusAndShowInputMethod;


/**
 * @author dss886
 * @since 2019-08-06
 */
public class SoftKeyboardResizeScene extends AppCompatScene implements Component.ClickedListener {

    private final int num6 = 6;
    private final int num2 = 2;
    private final int num8 = 8;
    private final int num20 = 20;

    private TextField editText;
    private Component parse;
    private RadioButton resize;
    private RadioButton pan;
    private RadioButton nothing;
    private final String boxColor = "#FFE337BC";


    @Override
    protected Component onCreateContentView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        parse = inflater.parse(ResourceTable.Layout_nav_ime_problems_layout, container, false);
        return parse;
    }

    @Override
    public void onAbilityCreated(Intent savedInstanceState) {
        super.onAbilityCreated(savedInstanceState);

        requireAbility().getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_RESIZE);
//        this.getView().setBackground(ColorUtil.getMaterialColor(2));
        editText = (TextField) parse.findComponentById(ResourceTable.Id_edit_text);
        resize = (RadioButton) parse.findComponentById(ResourceTable.Id_resize);
        pan = (RadioButton) parse.findComponentById(ResourceTable.Id_pan);
        nothing = (RadioButton) parse.findComponentById(ResourceTable.Id_nothing);
        radioOnDraw();
        resize.setClickedListener(this);
        pan.setClickedListener(this);
        nothing.setClickedListener(this);
        Button btn = (Button) parse.findComponentById(ResourceTable.Id_btn);
        btn.setText("只是一个按钮");
        btn.setClickedListener(this);
        setTitle("SOFT_INPUT_ADJUST");


    }

    private void radioOnDraw() {
        resize.addDrawTask((component, canvas) -> drawCheckBox3(component, canvas));
        pan.addDrawTask((component, canvas) -> drawCheckBox3(component, canvas));
        nothing.addDrawTask((component, canvas) -> drawCheckBox3(component, canvas));

    }

    /**
     * 多选 checkbox 外壳
     *
     * @param component
     * @param canvas
     */
    private void drawCheckBox3(Component component, Canvas canvas) {
        final float height = component.getHeight();
        Paint paint = new Paint();
        paint.setColor(new Color(Color.getIntColor(boxColor)));
        paint.setStyle(Paint.Style.STROKE_STYLE);
        paint.setStrokeWidth(num6);
        canvas.drawCircle(height / num2 - num8, height / num2, num20, paint);
    }

    @Override
    public void onResume() {
        super.onResume();
        focusAndShowInputMethod(editText);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()){
            case ResourceTable.Id_btn:
                SceneExtensions.getNavigationScene(this).onBackPressed();
                break;
            case ResourceTable.Id_resize:
                requireAbility().getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_RESIZE);
                break;
            case ResourceTable.Id_pan:
                requireAbility().getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_PAN);
                break;
            case ResourceTable.Id_nothing:
                requireAbility().getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_NOTHING);
                break;
            default:
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        requireAbility().getWindow().addFlags(WindowManager.LayoutConfig.INPUT_STATE_HIDDEN);
    }


}
